package co.edu.eam.iso.excel;

import java.io.FileInputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import co.edu.eam.iso.enums.CharacteristicEnum;
import co.edu.eam.iso.enums.SubCharacteristicEnum;
import co.edu.eam.iso.evaluated.CharacteristicEvaluated;
import co.edu.eam.iso.evaluated.ProductEvaluated;
import co.edu.eam.iso.evaluated.SubCharacteristicEvaluated;
import co.edu.eam.iso.model.Characteristic;
import co.edu.eam.iso.model.SubCharacteristic;

/**
 * @author Giovanny Ramirez
 */
public class ExcelReader {

	private static ExcelReader instance = null;

	public ProductEvaluated readFile(String fileName) throws Exception {
		FileInputStream fileInputStream = null;
		try {
			ProductEvaluated productEvaluated = new ProductEvaluated();
			fileInputStream = new FileInputStream(fileName);
			POIFSFileSystem poiFileSystem = new POIFSFileSystem(fileInputStream);
			HSSFWorkbook workBook = new HSSFWorkbook(poiFileSystem);
			HSSFSheet sheet = workBook.getSheetAt(0);
			
			Map<CharacteristicEnum, CharacteristicEvaluated> evaluation = new HashMap<CharacteristicEnum, CharacteristicEvaluated>();
			
			Iterator<Row> rowIterator = sheet.rowIterator();
			while (rowIterator.hasNext()) {
				HSSFRow row = (HSSFRow) rowIterator.next();
				Iterator<Cell> cellIterator = row.cellIterator();
				
				while (cellIterator.hasNext()) {
					HSSFCell cell = (HSSFCell) cellIterator.next();
					
					if ("Calidad producto".equals(cell.getStringCellValue())) {
						cell = (HSSFCell) cellIterator.next();
						productEvaluated.setQuality(new BigDecimal(cell.getStringCellValue()));
						
					} else if ("Característica".equals(cell.getStringCellValue())) {
						cell = (HSSFCell) cellIterator.next();
						
						CharacteristicEvaluated chrtcEvaluated = new CharacteristicEvaluated();
						Characteristic chrtc = new Characteristic();
						chrtc.setCharacteristicEnum(CharacteristicEnum.getByString(cell.getStringCellValue()));
						
						row = (HSSFRow) rowIterator.next();
						cellIterator = row.cellIterator();
						cellIterator.next();
						cell = (HSSFCell) cellIterator.next();							
						chrtcEvaluated.setQuality(new BigDecimal(cell.getStringCellValue()));
						
						row = (HSSFRow) rowIterator.next();
						cellIterator = row.cellIterator();
						cellIterator.next();
						cell = (HSSFCell) cellIterator.next();
						chrtc.setWeight(new BigDecimal(cell.getStringCellValue()));
						
						row = (HSSFRow) rowIterator.next();
						cellIterator = row.cellIterator();
						cellIterator.next();
						cell = (HSSFCell) cellIterator.next();
						chrtcEvaluated.setQualityVStotal(new BigDecimal(cell.getStringCellValue()));
						
						chrtcEvaluated.setCharacteristic(chrtc);
						evaluation.put(chrtc.getCharacteristicEnum(), chrtcEvaluated);
						
					} else if ("Subcaracterística".equals(cell.getStringCellValue())) {
						cell = (HSSFCell) cellIterator.next();
						
						SubCharacteristicEvaluated subchrtcEvaluated = new SubCharacteristicEvaluated();
						SubCharacteristic subchrtc = new SubCharacteristic();
						subchrtc.setSubCharacteristicEnum(SubCharacteristicEnum.getByString(cell.getStringCellValue()));
						
						row = (HSSFRow) rowIterator.next();
						cellIterator = row.cellIterator();
						cellIterator.next();
						cell = (HSSFCell) cellIterator.next();								
						subchrtcEvaluated.setQuality(new BigDecimal(cell.getStringCellValue()));
						
						row = (HSSFRow) rowIterator.next();
						cellIterator = row.cellIterator();
						cellIterator.next();
						cell = (HSSFCell) cellIterator.next();
						subchrtc.setWeight(new BigDecimal(cell.getStringCellValue()));
						
						subchrtcEvaluated.setSubCharacteristic(subchrtc);
						
						if (evaluation.get(subchrtc.getSubCharacteristicEnum().getCharacteristicEnum())
								.getSubCharacteristics() != null) {
							evaluation.get(subchrtc.getSubCharacteristicEnum().getCharacteristicEnum())
									.getSubCharacteristics().put(subchrtc.getSubCharacteristicEnum(), subchrtcEvaluated);
						} else {
							Map<SubCharacteristicEnum, SubCharacteristicEvaluated> subchrtcs = new HashMap<SubCharacteristicEnum, SubCharacteristicEvaluated>();
							subchrtcs.put(subchrtc.getSubCharacteristicEnum(), subchrtcEvaluated);
							evaluation.get(subchrtc.getSubCharacteristicEnum().getCharacteristicEnum()).setSubCharacteristics(subchrtcs);
						}
					}
				}
			}	
			
			productEvaluated.setCharacteristics(evaluation);
			return productEvaluated;
		} finally {
			if (fileInputStream != null) {
				fileInputStream.close();
			}
		}

	}

	public static ExcelReader getInstance() {
		if (instance == null) {
			instance = new ExcelReader();
		}
		return instance;
	}

}
